package com.pda.xinmazhi.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.pda.xinmazhi.R;
import com.pda.xinmazhi.bean.ScanBean;
import com.pda.xinmazhi.bean.ScanResultBean;
import com.pda.xinmazhi.bean.request.QRcodeRequest;
import com.pda.xinmazhi.net.NetUtils;
import com.pda.xinmazhi.view.ScanButView;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.dialog.base.BaseDialog;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.util.SharedPreferencesUtil;
import com.wangshen.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class CallUpDialog extends BaseDialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_pallet)
    TextView tvPallet;
    @BindView(R.id.iv_scan)
    ScanButView ivScan;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    CallUpConfirmClick callUpConfirmClick;
    public CallUpDialog(@NonNull Context context, CallUpConfirmClick callUpConfirmClick) {
        super(context);
        this.callUpConfirmClick=callUpConfirmClick;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        String storageBin= SharedPreferencesUtil.getInstance().getString(StringKey.STORAGEBIN,"");
        tvPallet.setText(storageBin);
        ivScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                NetUtils.geApi().scanTheQRCodeToVerify(request)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                            @Override
                            public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                                if (stringBaseAppEntity.getData().getType().equals("B")){
                                    tvPallet.setText(scanBean.getCode());
                                   // SharedPreferencesUtil.getInstance().putString(StringKey.STORAGEBIN,stringBaseAppEntity.getData().getCode());
                                }else {
                                    ToastUtils.showToast(getContext(),"请扫作业库位？");
                                }

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                ToastUtils.showToast(getContext(), throwable.getMessage());
                            }
                        });
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public int getView() {
        return R.layout.dialog_call_up;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                if (TextUtils.isEmpty(tvPallet.getText())){
                    ToastUtils.showToast(getContext(), "库位不能为空！");
                    return;
                }
                callUpConfirmClick.onConfrim(tvPallet.getText().toString());
                dismiss();
                break;
        }
    }
    public interface CallUpConfirmClick{
        void onConfrim(String pallet);
    }
}
